package com.huilai.platform.country_platform.repository;

import com.huilai.platform.country_platform.dto.RecruitmentDTO;
import com.huilai.platform.country_platform.model.InformationRecruitment;
import com.huilai.platform.country_platform.model.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import javax.transaction.Transactional;
import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;

/**
 * 作者：洪霓
 * 日期：2023/2/18
 * 注释：
 */
public interface InformationRecruitmentReposity extends JpaRepository<InformationRecruitment, Integer>, JpaSpecificationExecutor<InformationRecruitment> {

//    @Query("select new com.huilai.platform.country_platform.dto.RecruitmentDTO(r.address,r.image,r.address,r.telephone,r.name,st.statusstring,r.describe, r.createTime, r.updateTime) from InformationRecruitment r left join StatusMap st on r.status = st.status where r.id = ?1")
//    RecruitmentDTO getRecruitmentDTOById(String id);

//    @Query(value = "select ir from InformationRecruitment ir " +
//            "left join UserRecruitment ur on ir.id = ur.recruitmentId " +
//            "left join User u on ur.userId = u.id " +
//            "where u.id = :id")
    @Transactional
    @Query(value = "select * from information_recruitment ir " +
            "where ir.recruitment_id in (select ur.recruitment_id from user_recruitment ur where ur.user_id = :id)", nativeQuery = true)
    List<InformationRecruitment> findInformationRecruitmentByUserId(Integer id);

    @Transactional
    @Query(value = "select * from information_recruitment ir " +
            "where ir.audit_status !=0 and ir.recruitment_id in (select ur.recruitment_id from user_recruitment ur where ur.user_id = :id)", nativeQuery = true)
    List<InformationRecruitment> findAuditInformationRecruitmentByUserId(Integer id);

    @Modifying(clearAutomatically = true)
    @Transactional
    @Query(value = "insert into information_recruitment(company, image, address, telephone, name, status, audit_status, `describe`) " +
            "values(:#{#informationRecruitment.company}, :#{#informationRecruitment.image}, :#{#informationRecruitment.address}, :#{#informationRecruitment.telephone}, :#{#informationRecruitment.name}, 0, 0, :#{#informationRecruitment.describe}) ", nativeQuery = true)
    Integer addRecruitment(InformationRecruitment informationRecruitment);

    @Modifying(clearAutomatically = true)
    @Transactional
    @Query(value = "insert into information_recruitment(company, image, address, telephone, name, status, audit_status, `describe`) " +
            "values(:#{#informationRecruitment.company}, :#{#informationRecruitment.image}, :#{#informationRecruitment.address}, :#{#informationRecruitment.telephone}, :#{#informationRecruitment.name}, :#{#informationRecruitment.status}, :#{#informationRecruitment.auditStatus}, :#{#informationRecruitment.describe}) ", nativeQuery = true)
    Integer addRecruitmentAll(InformationRecruitment informationRecruitment);

    InformationRecruitment findInformationRecruitmentByDescribe(String describe);

    @Modifying(clearAutomatically = true)
    @Transactional
    @Query(value = "update information_recruitment ir set ir.company = :#{#informationRecruitment.company}, " +
            "ir.address = :#{#informationRecruitment.address}, " +
            "ir.name = :#{#informationRecruitment.name}," +
            "ir.telephone = :#{#informationRecruitment.telephone}, " +
            "ir.image = :#{#informationRecruitment.image}, " +
            "ir.describe = :#{#informationRecruitment.describe}," +
            "ir.status = :#{#informationRecruitment.status}," +
            "ir.audit_status = 0 where ir.recruitment_id = :#{#informationRecruitment.id}",nativeQuery = true)
    Integer updateRecruitment(InformationRecruitment informationRecruitment);

    @Modifying(clearAutomatically = true)
    @Transactional
    @Query(value = "update information_recruitment ir set ir.company = :#{#informationRecruitment.company}, " +
            "ir.address = :#{#informationRecruitment.address}, " +
            "ir.name = :#{#informationRecruitment.name}," +
            "ir.telephone = :#{#informationRecruitment.telephone}, " +
            "ir.image = :#{#informationRecruitment.image}, " +
            "ir.describe = :#{#informationRecruitment.describe}," +
            "ir.status = :#{#informationRecruitment.status} where ir.recruitment_id = :#{#informationRecruitment.id}",nativeQuery = true)
    Integer updateRecruitmentAll(InformationRecruitment informationRecruitment);

    @Modifying(clearAutomatically = true)
    @Transactional
    @Query("delete from InformationRecruitment ir where ir.id = :id")
    Integer deleteRecruitmentById(Integer id);

    @Modifying(clearAutomatically = true)
    @Transactional
    @Query(value = "update information_recruitment ir set ir.audit_status = :auditStatus where ir.recruitment_id = :id",nativeQuery = true)
    Integer updateAuditStatus(Integer id, Integer auditStatus);
}
